<?php

namespace App\Daos\Admin\Users;

use App\Models\Users\Genre;
use App\Models\Users\Tag;
use App\Models\Users\Users;

class TagDao
{
    public static function tagList ($size,$offset,$type){
        $data['total'] = Tag::when($type,function ($query,$type){
            $query->where('class_id',$type);
        })->get()->count();
        $data['class'] = Genre::orderBy('sort')->get(['id','name']);
        $data['data'] = Tag::with('class')->when($type,function ($query,$type){
            $query->where('class_id',$type);
        })->offset($offset)->limit($size)->get();
        foreach ($data['data'] as &$value){
            $value['type'] = $value['class']['name'];
        }
        return $data;
    }

    public static function tagAdd ($data){
        return Tag::create([
            'class_id' => $data['id'],
            'name' => $data['name']
        ]);
    }

    public static function classAdd ($data){
        return Genre::create([
            'name' => $data['name'],
            'sort' => $data['sort']
        ]);
    }

    public static function tagUpdate ($data){
        $res = Tag::where('id',$data['class_id'])->update([
            'name' => $data['name'],
            'class_id' => $data['class_id'],
            'updated_at' => date('Y-m-d H:i:s')
        ]);
        return $res;
    }

    public static function tagDelete ($id){
        $res = Tag::find($id);
        $res->delete();
        return $res;
    }

    public static function sendTag ($user,$tag){
        return Users::whereIn('id',$user)->update([
            'tag' => $tag,
            'updated_at' => date('Y-m-d H:i:s')
        ]);
    }
}
